package com.haojiang.mh.ui.adapter

import android.content.Context
import android.net.wifi.ScanResult
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.haojiang.mh.R


/**
 * Created by ZapFive on 2019-05-20.
 *
 *
 * wuzhuang@mirahome.me
 */
class WifiListAdapter(private val beanList: List<ScanResult>?, private val ctx: Context) :
    RecyclerView.Adapter<WifiListAdapter.MyHolder>() {
    private var listener: OnItemChildClickListener? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyHolder {
        val view = LayoutInflater.from(ctx).inflate(R.layout.item_wifi_list, parent, false)
        return MyHolder(view)
    }

    override fun onBindViewHolder(holder: MyHolder, position: Int) {
        if (position == 0) {
            holder.view_divide_line.visibility = View.INVISIBLE
        } else {
            holder.view_divide_line.visibility = View.VISIBLE
        }
        holder.tv_item_wifi.text = beanList!![position].SSID
        holder.itemView.setOnClickListener { v ->
            if (listener == null) return@setOnClickListener
            val adapterPosition = holder.adapterPosition
            listener?.onItemClick(v, beanList[adapterPosition], adapterPosition)
        }
    }

    override fun getItemCount(): Int {
        return beanList?.size ?: 0
    }

    fun setItemSelectedListener(listener: OnItemChildClickListener) {
        this.listener = listener
    }

    inner class MyHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var tv_item_wifi: TextView = itemView.findViewById(R.id.tv_item_wifi)
        var view_divide_line: View = itemView.findViewById(R.id.view_divide_line)
    }
}
